﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DVSProject.Lib;
using DVSProject.Models;

namespace DVSProject.Controllers
{
    public class ComplainController : Controller
    {
        //
        // GET: /Complain/

        public ActionResult Index()
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            ComplainRepo complainRepo = new ComplainRepo();
            IEnumerable<ComplainModels> complains = complainRepo.GetComplain();
            return View(complains);
        }

        //
        // GET: /Complain/Details/5

        public ActionResult Details(int id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            ComplainRepo complainRepo = new ComplainRepo();
            ComplainModels complain = complainRepo.GetComplainById(id);

            return View(complain);
        }

        //
        // GET: /Complain/Create


        public ActionResult Create()
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            LocationSetRepo customerRepo = new LocationSetRepo();
            IEnumerable<LocationSetModels> locationSets = customerRepo.GetLocationSet();

            string selectedLocationCode = "";
            IEnumerable<SelectListItem> selectlist = from s in locationSets
                                                     select new SelectListItem
                                                     {
                                                         Selected = (s.LocationSetCode == selectedLocationCode),
                                                         Text = s.LocationSetCode + ' ' + s.LocSetNameTh,
                                                         Value = s.LocationSetCode.ToString()
                                                     };

            var complain = new ComplainModels { locationSets = locationSets, locationSetSelect = selectlist };
            return View(complain);
        }

        //
        // POST: /Complain/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add insert logic here
                ComplainModels complain = new ComplainModels();
                complain.LocationSetCode = collection["locationSets"];
                complain.FSR_No = collection["FSR_No"];
                complain.CreateBy = (string)Session["User"]; 
                ComplainRepo complainRepo = new ComplainRepo();
                bool result = complainRepo.Create(complain);

                if (result)
                {
                    return RedirectToAction("SelectMachine",complain);
                    
                }
                return RedirectToAction("Create");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult SelectMachine(ComplainModels complainParam)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            LocationSetRepo customerRepo = new LocationSetRepo();

            LocationMachineSetRepo locationMachineSetRepo = new LocationMachineSetRepo();
            IEnumerable<LocationMachineSetModels> locationMachineSets = locationMachineSetRepo.GetLocationMachineSet().Where(l => l.LocationSetCode == complainParam.LocationSetCode);

            string selectedMachine = "";
            IEnumerable<SelectListItem> selectlist = from s in locationMachineSets
                                                     select new SelectListItem
                                                     {
                                                         Selected = (s.SerialNo == selectedMachine),
                                                         Text =  s.Machine_Name,
                                                         Value = s.SerialNo.ToString()
                                                     };

            var complain = new ComplainModels { locationMachineSets = locationMachineSets, machineSelect = selectlist };
            return View(complain);
        }




        [HttpPost]
        public ActionResult SelectMachine(FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {

                string SerialNo = collection["locationMachineSets"];
                string _userid = (string)Session["User"];

                ComplainRepo complainRepo = new ComplainRepo();
                bool result = complainRepo.UpdateMachine(SerialNo, _userid);

                if (result)
                {
                    return RedirectToAction("ComplainForm");
                }
                return RedirectToAction("SelectMachine");
            }
            catch
            {
                return View();
            }
        }


        public ActionResult ComplainForm(string Id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            return View();
        }

        [HttpPost]
        public ActionResult ComplainForm(string Id,FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            ComplainModels complain = new ComplainModels();
           // complain.CustId = collection["CustId"];
            complain.FSR_No = Id;
            complain .Punctual = Convert.ToInt32( collection["Punctual"]);
            complain.Service = Convert.ToInt32(collection["Service"]);
            complain.Cooperation = Convert.ToInt32(collection["Cooperation"]);
            complain.Relation = Convert.ToInt32(collection["Relation"]);
            complain.Result = Convert.ToInt32(collection["Result"]);
            complain.UpdateBy = (string)Session["User"]; 

            ComplainRepo complainRepo = new ComplainRepo();
            bool result = complainRepo.Edit(complain);

            if (result)
            {
                return RedirectToAction("Problem");
            }
            return RedirectToAction("ComplainForm");
           
        }

        public ActionResult Problem()
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            return View();
        }

         [HttpPost]
        public ActionResult Problem(FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            ComplainModels complain = new ComplainModels();
            complain.Problem = collection["Problem"];
            complain.ProblemSolv = collection["ProblemSolv"];
            complain.UpdateBy = (string)Session["User"];
            ComplainRepo complainRepo = new ComplainRepo();
            bool result = complainRepo.UpdateProblem(complain);

            if (result)
            {
                return RedirectToAction("../Access/Index");
            }
            return RedirectToAction("Problem");
            
        }


       

        //
        // GET: /Complain/Edit/5

        public ActionResult Edit(int id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            return View();
        }

        //
        // POST: /Complain/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Complain/Delete/5

        public ActionResult Delete(int id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            ComplainRepo complainRepo = new ComplainRepo();
            ComplainModels complain = complainRepo.GetComplainById(id);
            return View(complain);
        }

        //
        // POST: /Complain/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add delete logic here
                ComplainRepo complainRepo = new ComplainRepo();
                string _userid = (string)Session["User"];
                bool result = complainRepo.Delete(id, _userid);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
